package org.fujene.sandbox;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;

import junit.framework.TestCase;

public class TestDisk extends TestCase{
	public static void testDisk() throws IOException{
		// prepare for making test files
		File testfile = new File("TESTFILE");
		if(!testfile.exists()) testfile.createNewFile();
		FileOutputStream inputs = null;
		// write 10MB
		try{
			inputs = new FileOutputStream(testfile);
			inputs.write(new byte[10000000]);
		}finally{
			if(inputs != null) inputs.close();
		}

		FileInputStream stream = null;
		long time;
		try{
//			byte[] readb = new byte[50];
			time = System.nanoTime();
			stream = new FileInputStream(testfile);
			for(int i = 0; i < 10000; i++){
				stream.skip(500);
//				stream.read(readb);
			}
			System.out.println("InputStream Done.  Time = " + ((System.nanoTime() - time) / 1000000.0)
					+ " ms");
		}finally{
			if(stream != null) stream.close();
		}

		RandomAccessFile random = null;
		try{
//			byte[] readb = new byte[50];
			time = System.nanoTime();
			random = new RandomAccessFile(testfile, "r");
			for(int i = 0; i < 10000; i++){
				random.seek((i + 1) * 500);
//				random.read(readb);
			}
			System.out.println("RandomAccess Done. Time = " + ((System.nanoTime() - time) / 1000000.0)
					+ " ms");
		}finally{
			if(random != null) random.close();
		}

		testfile.deleteOnExit();
	}
}
